package com.business.yang.core;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.widget.ImageView;
import android.widget.TextView;

import com.business.yang.R;
import com.business.yang.mvp.view.home.activity.HomeActivity;
import com.lyxs916.appbaselibrary.core.activity.BaseActivity;

import java.util.Random;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;


/**
 * 说明:欢迎的页面
 * 作者： 杨阳; 创建于：  2017-06-06  11:33
 */
public class WelcomeActivity extends BaseActivity {
    @BindView(R.id.background)
    ImageView mBackground;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.version)
    TextView version;
    @BindView(R.id.copyright)
    TextView copyright;

    private final int mAnimationDuration = 500;
    private final float mScaleEnd = 1.13f;


    private int[] mBackgroundArray = {
            R.mipmap.splash1,
            R.mipmap.splash2,
            R.mipmap.splash3,
            R.mipmap.splash4,
    };

    @Override
    public int setContentView() {
        return R.layout.base_activity_welcome;
    }

    @Override
    public void parseIntent(Intent intent) {

    }

    @Override
    public void initView() {
        mBackground.setImageResource(mBackgroundArray[new Random().nextInt(mBackgroundArray.length)]);
        animateImage();
    }

    /**
     * 添加动画
     */
    private void animateImage() {
        ObjectAnimator animatorX = ObjectAnimator.ofFloat(mBackground, "scaleX", 1f, mScaleEnd);
        ObjectAnimator animatorY = ObjectAnimator.ofFloat(mBackground, "scaleY", 1f, mScaleEnd);

        AnimatorSet set = new AnimatorSet();
        set.setDuration(mAnimationDuration).play(animatorX).with(animatorY);
        set.start();

        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                finishActivity();
            }
        });
    }

    private void finishActivity() {
        Observable.timer(10, TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long aLong) {
                        HomeActivity.startUi(mActivity);
                        finish();
                    }
                });
    }

    @Override
    public void onBackPressed() {
//        super.onBackPressed();
    }
}
